
//
//  MWGoodsDetailsBriefCell.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/25.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWGoodsDetailsBriefCell.h"
#import "MWGoodsDetailsBriefPictureCell.h"

@interface MWGoodsDetailsBriefCell () <UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) UIImageView *pictureIV;
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) UILabel *titleLB;
@property(nonatomic, strong) UILabel *priceLB;

@property(nonatomic, assign) NSInteger currentPosition;
@property(nonatomic, strong) NSMutableArray *urlArray;

@end

@implementation MWGoodsDetailsBriefCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupPageSubviews];
        return self;
    }
    return self;
}

- (void)setupData:(MWGoods *)goods {
    NSString *urlStr = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,h_320", goods.image];
    [_pictureIV sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"infoHeard"]];
    [self.urlArray removeAllObjects];
    [self.urlArray addObjectsFromArray:goods.slider_image];
    _titleLB.text = goods.store_name;
    _titleLB.backgroundColor = MWHexColor(@"#f2f2f2");
   
    if (goods.price && goods.price.length > 0) {
        _priceLB.text = [NSString stringWithFormat:@"¥%@",  goods.price];
    }
   
    [_collectionView reloadData];
}

- (void)doubleTap:(UITapGestureRecognizer *)sender {
    NSMutableArray *datas = [NSMutableArray array];
    for (int i = 0; i < self.urlArray.count; i++) {
        YBIBImageData *browseModel = [[YBIBImageData alloc] init];
        browseModel.allowSaveToPhotoAlbum = YES;
        browseModel.imageURL = self.urlArray[i];
        browseModel.defaultLayout.maxZoomScale = 3;
        [datas addObject:browseModel];
    }
    YBImageBrowser *browser = [[YBImageBrowser alloc] init];
//    if ([MWConfigManager manager].language == 0) {
//        [YBIBCopywriter sharedCopywriter].type = YBIBCopywriterTypeSimplifiedChinese;
//    } else {
//        [YBIBCopywriter sharedCopywriter].type = YBIBCopywriterTypeEnglish;
//    }
    browser.defaultToolViewHandler.topView.operationType = YBIBTopViewOperationTypeSave;
    browser.dataSourceArray = datas;
    browser.currentPage = _currentPosition;
    [browser show];
}

#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.urlArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MWGoodsDetailsBriefPictureCell";
    MWGoodsDetailsBriefPictureCell *pictureCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    pictureCell.block = ^{
        NSMutableArray *datas = [NSMutableArray array];
        for (int i = 0; i < self.urlArray.count; i++) {
            YBIBImageData *browseModel = [[YBIBImageData alloc] init];
            browseModel.allowSaveToPhotoAlbum = YES;
            browseModel.imageURL = self.urlArray[i];
            [datas addObject:browseModel];
        }
        
        YBImageBrowser *browser = [[YBImageBrowser alloc] init];
        browser.defaultToolViewHandler.topView.operationType = YBIBTopViewOperationTypeSave;
        browser.dataSourceArray = datas;
//        if ([MWConfigManager manager].language == 0) {
//            [YBIBCopywriter sharedCopywriter].type = YBIBCopywriterTypeSimplifiedChinese;
//        } else {
//            [YBIBCopywriter sharedCopywriter].type = YBIBCopywriterTypeEnglish;
//        }
        browser.currentPage = indexPath.row; 
        [browser show];
    };
    
    [pictureCell setUrlPath:self.urlArray[indexPath.row]];
    return pictureCell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(16, 16, 16, 16);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(68, 68);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _currentPosition = indexPath.row;
   
    NSString *urlStr = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,h_320", self.urlArray[indexPath.row]];
    [_pictureIV sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"infoHeard"]];
}

- (NSMutableArray *)urlArray {
    if (!_urlArray) {
        _urlArray = [[NSMutableArray alloc] init];
    }
    
    return _urlArray;
}

- (void)setupPageSubviews {
    UITapGestureRecognizer *doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
//    doubleRecognizer.numberOfTapsRequired = 2;
    
    _pictureIV = [[UIImageView alloc] init];
    _pictureIV.contentMode = UIViewContentModeScaleAspectFit;
    _pictureIV.userInteractionEnabled = YES;
    [_pictureIV addGestureRecognizer:doubleRecognizer];
    [self.contentView addSubview:_pictureIV];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[MWGoodsDetailsBriefPictureCell class] forCellWithReuseIdentifier:@"MWGoodsDetailsBriefPictureCell"];
    [self.contentView addSubview:_collectionView];
    
    _titleLB = [[UILabel alloc] init];
    _titleLB.layer.cornerRadius = 5;
    _titleLB.clipsToBounds = YES;
    _titleLB.font = MWFont(18);
    _titleLB.textColor = MWHexColor(@"#333333");
    _titleLB.textAlignment = NSTextAlignmentCenter;;
    [self.contentView addSubview:_titleLB];
    
    _priceLB = [[UILabel alloc] init];
    _priceLB.font = MWFont(20);
    _priceLB.textColor = MWHexColor(@"#E42A2A");
    [self.contentView addSubview:_priceLB];
    
    [_pictureIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.height.equalTo(@160);
    }];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pictureIV.mas_bottom);
        make.left.right.equalTo(self.contentView);
        make.height.equalTo(@100);
    }];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionView.mas_bottom);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
        make.height.equalTo(@45);
    }];
    
    [_priceLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLB.mas_bottom).offset(15);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
}


@end
